package com.slg.android.listenbook.biz.bookcity.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.bookcity.adapter.RecommendAdapter;
import com.slg.android.listenbook.configuration.Constants;
import com.slg.android.listenbook.moudle.RecommenList;
import com.slg.android.listenbook.net.http.HttpClientProxy;
import com.slg.android.listenbook.net.http.JsonCallBack;

public class RecommandFragment extends Fragment implements PullToRefreshGridView.OnRefreshListener2, JsonCallBack {

    private RecommenList list;
    private RecommendAdapter adapter;
    private HttpClientProxy httpClientProxy;
    private GridView trueGrid;


    public RecommandFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommand, container, false);
        PullToRefreshGridView refreshGridView = (PullToRefreshGridView)
                view.findViewById(R.id.recommend_refresh);
        refreshGridView.setOnRefreshListener(this);
        trueGrid = refreshGridView.getRefreshableView();
        list = new RecommenList();
        httpClientProxy = new HttpClientProxy(Constants.RECOMMEN_URL + Constants.TOKEN_MID);
        httpClientProxy.doGet(null, this, 1);

        return view;
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        if (adapter != null) {
            if (httpClientProxy != null) {
                httpClientProxy.doGet(null, this, 2);
                adapter.notifyDataSetChanged();
            }

        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {


    }

    @Override
    public void onSuccess(ResponseInfo<String> responseInfo, int requestCode) {
        Gson gson = new Gson();
        String str = responseInfo.result;
        if (str != null && requestCode == 1) {
            RecommenList innerList = gson.fromJson(str, RecommenList.class);
            list = innerList;
            adapter = new RecommendAdapter(getActivity(), list);
            trueGrid.setAdapter(adapter);
            Log.d("success", list.toString());
        } else if (str != null && requestCode == 2) {
            RecommenList innerList = gson.fromJson(str, RecommenList.class);
            innerList.getList();
            list.getList().addAll(innerList.getList());
            Log.d("success", list.toString());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(HttpException error, String msg, int requestCode) {

    }

    @Override
    public void onStart(int requestCode) {

    }

    @Override
    public void onCancelled(int requestCode) {

    }

    @Override
    public void onLoading(long total, long current, boolean isUploading, int requestCode) {

    }
}
